import { useAirCityStore } from '@/stores/aircity'
import { useToolsStore } from '@/stores/tools'
import { toTree } from '@/components/tools/layerTreeUtils'
import { useAnaphaseStore } from '@/stores/anaphase'

const ToolsStore: any = useToolsStore()
const AirCityStore: any = useAirCityStore()
const AnaphaseStore: any = useAnaphaseStore()

/**
 * 图层树
 */
export const getLayerTree = () => {
  return new Promise(async (resolve) => {
    if (ToolsStore.infotree) {
      return resolve(ToolsStore.infotree)
    }
    const res: any = await __g.infoTree.get()
    await AirCityStore.SetTreeInfo(res.infotree)

    await ToolsStore.Setinfotree(toTree(res.infotree))
    return resolve(ToolsStore.infotree)
  })
}

/**
 * 获取导览列表
 */
export const getAnimationList = () => {
  return new Promise((resolve) => {
    if (ToolsStore.animationList.length != 0) {
      return resolve(ToolsStore.animationList)
    }
    // 获取导览列表
    __g.camera.getAnimationList().then(async (animationList: any) => {
      const imgs = await Promise.all(
        animationList.data.map((item: any) =>
          __g.camera.getAnimationImage(item.name)
        )
      )
      animationList.data.forEach((item: any, index: number) => {
        item.image = (imgs[index] as any).image
      })

      ToolsStore.setAnimationList(animationList.data)
    })
    resolve(ToolsStore.animationList)
  })
}

/**
 * 获取后期对象
 */
export const getPostProcessMode = async () => {
  return new Promise(async (resolve, reject) => {
    if (Object.keys(AnaphaseStore.tonemapperOption).length == 0) {
      return resolve(AnaphaseStore.$state)
    }
    // 获取后期参数
    const {
      tonemapper,
      // 对比度
      contrast,
      // 饱和度
      saturation,
      // 泛光
      bloomIntensity,
      // 环境光遮罩
      ambientIntensity,
      // LUT效果
      lutMode,
      // 强度
      lutIntensity,
      // 滤镜效果
      postProcessEffects,
      // 线宽
      wireThickness,
      darkCorner,
      lensFlareIntensity,
      screenPercentage,
      terrainGlobalAlpha,
      terrainGlobalLitStatus,
      osgbGlobalLitStatus,
      osgbGlobalAlpha,
      antiAliasing,
      dofMode,
      receiveDecalMode,
    } = await __g.settingsPanel.getPostProcessMode()
    let bool = tonemapper != 0
    AnaphaseStore.setTonemapperOption({
      tonemapper: bool,
      // 对比度
      contrast,
      // 饱和度
      saturation,
      // 泛光
      bloomIntensity,
      // 环境光遮罩
      ambientIntensity,
      darkCorner,
      lensFlareIntensity,
      screenPercentage,
      terrainGlobalAlpha: 1,
      terrainGlobalLitStatus: true,
      osgbGlobalLitStatus: false,
      osgbGlobalAlpha: 1,
      antiAliasing: true,
      dofMode: 0,
      postProcessEffects: 0,
      receiveDecalMode,
    })
    AnaphaseStore.setLutOption({
      // LUT效果
      lutMode,
      // 强度
      lutIntensity,
    })
    AnaphaseStore.setProcessOption({
      // 滤镜效果
      postProcessEffects,
      // 线宽
      wireThickness,
    })
    return resolve(AnaphaseStore.$state)
  })
}

/**
 * 获取图层树对象
 * @returns
 */
export async function getLayerTreeObject() {
  return new Promise(async (resolve, reject) => {
    if (Object.keys(AirCityStore.layerTreeObject).length) {
      return resolve(AirCityStore.layerTreeObject)
    }

    const res: any = await __g.infoTree.get()
    // 图层树
    const layerTreeObject: any = {}
    res.infotree.forEach((item: any) => {
      layerTreeObject[item.name] = item.iD
    })
    AirCityStore.SetTreeInfo(res.infotree)
    AirCityStore.SetLayerTreeObject(layerTreeObject)
    return resolve(layerTreeObject)
  })
}
